package com.tinyideatech.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyideatech.system.aspect.SysLogServiceAspect;
import com.tinyideatech.system.entity.CarGoods;
import com.tinyideatech.system.mapper.CarGoodsMapper;
import com.tinyideatech.system.service.ICarGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author zhangy-5
 * @date 2020-04-07
 */
@Service
public class CarGoodsServiceImpl extends ServiceImpl<CarGoodsMapper, CarGoods> implements ICarGoodsService {

    @Autowired
    private CarGoodsMapper carGoodsMapper;

    @Override
    @SysLogServiceAspect(description = "更改状态")
    public void changeStatus(CarGoods carGoods) {

        CarGoods goods = getById(1L);
        if (goods != null) {
            goods.setStatus(carGoods.getStatus());
            updateById(goods);
        }
    }

    @Override
    @SysLogServiceAspect(description = "获取商品价格")
    public Integer getPrice(int id) {
        CarGoods carGoods = carGoodsMapper.selectOne(new QueryWrapper<CarGoods>().eq("id", id));
        Double testPrice = 0.01;
        if (carGoods == null) {
            return Yuan2Fen(testPrice);
        }
        return Yuan2Fen(carGoods.getPrice());
    }

    @Override
    @SysLogServiceAspect(description = "获取商品名称")
    public String getName(int id) {
        CarGoods carGoods = carGoodsMapper.selectOne(new QueryWrapper<CarGoods>().eq("id", id));
        if (carGoods == null) {
            return "";
        }
        return carGoods.getName();
    }

    private static Integer Yuan2Fen(Double yuan) {
        return new BigDecimal(String.valueOf(yuan)).movePointRight(2).intValue();
    }
}
